package com.hae.shop.micoshopportalweb.controller.index;

import com.hae.shop.common.base.Response;
import com.hae.shop.member.model.respDto.UserLoginRespDto;
import com.hae.shop.micoshopportalweb.controller.member.feign.MemberFeign;
import com.hae.shop.micoshopportalweb.utils.CookieUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * 主页相关
 */
@Controller
public class IndexController {

    private static final String INDEX_FTL = "index";
    @Autowired
    private MemberFeign memberFeign;

    /**
     * 跳转到首页
     *
     *
     * @return
     */
    @RequestMapping("/index.html")
    public String indexHtml() {
        return INDEX_FTL;
    }

    /**
     *
     * 主页进入程序
     * @return
     */
    @RequestMapping("/")
    public String index(HttpServletRequest request, Model model) {
        // 1.获取浏览器token
        String token = CookieUtils.getCookieValue(request, "mico_token");
        if (!StringUtils.isEmpty(token)) {
            // 2.token不空时，调用member服务根据token查询用户
            Response<UserLoginRespDto> userLoginRespDtoResponse = memberFeign.loginBytoken(token);
            if (userLoginRespDtoResponse.isSuccess()) {
                // 查询成功则将member信息放入页面并返回（如果是号码请将中间几位替换为***）
                String userid = String.valueOf(userLoginRespDtoResponse.getResult().getUserid());
                model.addAttribute("member_id", userid);
            }
        }
        return INDEX_FTL;
    }
}
